package com.xuegao.xuegaotool;

import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @author xuegao
 * @version 1.0
 * @date 2021/12/29 16:03
 */
public class FileDeal {
    public static final String PDF_COMMON = "pdf";

    private static String createFile1(JPanel panel, File file) {
        String absolutePath = file.getAbsolutePath();
        // System.out.println(absolutePath);

        String absolutePath1 = file.getParentFile().getAbsolutePath();
        // System.out.println(absolutePath1);

        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss"));
        String newFilePath = file.getParentFile().getAbsolutePath() + "\\" + format + "\\";
        File file1 = new File(newFilePath);
        boolean mkdirs = file1.mkdirs();
        if (Boolean.FALSE.equals(mkdirs)) {
            JOptionPane.showMessageDialog(panel, "创建文件夹失败，没有权限创建", "转换已完成", JOptionPane.INFORMATION_MESSAGE);
        }
        return newFilePath;
    }

    private static String createFile2(JPanel panel, File file) {
        String absolutePath = file.getParentFile().getAbsolutePath();
        System.out.println(absolutePath);

        String absolutePath1 = file.getParentFile().getParentFile().getAbsolutePath();
        System.out.println(absolutePath1);

        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss"));
        String newFilePath = file.getParentFile().getParentFile().getAbsolutePath() + "\\" + format + "\\";
        File file1 = new File(newFilePath);
        boolean mkdirs = file1.mkdirs();
        if (Boolean.FALSE.equals(mkdirs)) {
            JOptionPane.showMessageDialog(panel, "创建文件夹失败，没有权限创建", "转换已完成", JOptionPane.INFORMATION_MESSAGE);
        }
        return newFilePath;
    }

    /***
     * 在当前文件夹创建一个文件
     *
     * @param panel:
     * @param file:
     * @return java.lang.String
     * @author xuegao
     * @date 2021/12/29 16:07
     */
    public static String createFileV3(JPanel panel, File file) {
        String newFilePath = "";
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss"));
        if (file.isDirectory()) {
            newFilePath = file.getAbsolutePath() + "\\" + format + "\\";
        } else {
            newFilePath = file.getParentFile().getAbsolutePath() + "\\" + format + "\\";
        }
        File file1 = new File(newFilePath);
        boolean mkdirs = file1.mkdirs();
        if (Boolean.FALSE.equals(mkdirs)) {
            JOptionPane.showMessageDialog(panel, "创建文件夹失败，没有权限创建", "转换已完成", JOptionPane.INFORMATION_MESSAGE);
        }
        return newFilePath;
    }

    /***
     * 根据读取到的文件名称，创建一个文件名称的文件夹
     *
     * @param panel:
     * @param file:
     * @return java.lang.String
     * @author xuegao
     * @date 2021/12/29 16:07
     */
    public static String createFileV4(JPanel panel, File file) {
        String newFilePath = "";
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss"));
        if (file.isDirectory()) {
            newFilePath = file.getAbsolutePath() + "\\" + getFileNameNoSuffix(file.getName()) + " " + format + "\\";
        } else {
            newFilePath = file.getParentFile().getAbsolutePath() + "\\" + getFileNameNoSuffix(file.getName()) + " " + format + "\\";
        }
        File file1 = new File(newFilePath);
        boolean mkdirs = file1.mkdirs();
        if (Boolean.FALSE.equals(mkdirs)) {
            JOptionPane.showMessageDialog(panel, "创建文件夹失败，没有权限创建", "转换已完成", JOptionPane.INFORMATION_MESSAGE);
        }
        return newFilePath;
    }

    /**
     * <br/> @Title: 获取文件的后缀
     * <br/> @MethodName:  getSuffix
     * <br/> @param fileName:
     * <br/> @Return java.lang.String
     * <br/> @Description:
     * <br/> @author: xuegao
     * <br/> @date:  2020/8/3 17:27
     */
    private static String getSuffix(String fileName) {
        if (fileName.lastIndexOf(".") > 0) {
            // 统一 处理返回 小写
            return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        }
        return null;
    }

    public static void main(String[] args) {
        String fileName = "跨越集团【人】字（通知）2022-001号关于2022年元旦放假的通知 - 副本.pdf";

    }

    /**
     * <br/> @Title: 去除文件后缀
     */
    public static String getFileNameNoSuffix(String fileName) {
        if (fileName.lastIndexOf(".") > 0) {
            String[] split = fileName.split("\\.");
            return split[0];
        }
        return null;
    }

    /**
     * <br/> @Title: 获取文件的后缀
     * <br/> @MethodName:  getSuffix
     * <br/> @param fileName:
     * <br/> @Return java.lang.String
     * <br/> @Description:
     * <br/> @author: xuegao
     * <br/> @date:  2020/8/3 17:27
     */
    public static boolean isSuffixEquals(File file, String suffix) {
        if (StringUtils.isBlank(suffix)) {
            throw new RuntimeException("文件后缀不能为空");
        }
        String str = getSuffix(file.getName());
        if (StringUtils.isNotBlank(str)) {
            return Objects.equals(getSuffix(file.getName()), suffix);
        }
        return false;
    }
}